package com.rxyl.love.petdoctor.acticity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.rxyl.love.petdoctor.R;
import com.rxyl.love.petdoctor.adapter.WelcomePagerAdapter;
import com.rxyl.love.petdoctor.util.SharedUtil;


@ContentView(R.layout.activity_welcome)
public class WelcomeActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    @ViewInject(R.id.vp_welcome)
    private ViewPager viewPager;

    @ViewInject(R.id.btn_startMainActivity)
    private Button btn_start;

    @ViewInject(R.id.rg_welcome)
    private RadioGroup radioGroup;

    private boolean isFirstRun = true;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        initDatas();
        initViews();

        viewPager.addOnPageChangeListener(this);
        btn_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(WelcomeActivity.this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getApplicationContext().startActivity(intent);

                SharedUtil.putBoolean("isFirstRun", false);

                WelcomeActivity.this.finish();
            }
        });

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                viewPager.setCurrentItem(checkedId);
            }
        });
    }


    /**
     * 初始化数据
     */
    private void initDatas() {

        isFirstRun = SharedUtil.getBoolean("isFirstRun");
    }

    // 数据源资源ID数组
    private int[] mImageIds = {R.mipmap.loading_page1, R.mipmap.loading_page2, R.mipmap.loading_page3};
    private WelcomePagerAdapter adapter;

    /**
     * 初始化视图
     */
    private void initViews() {
        if (isFirstRun) {
            radioGroup.setVisibility(View.VISIBLE);
            adapter = new WelcomePagerAdapter(mImageIds, this);
            viewPager.setAdapter(adapter);
            viewPager.setCurrentItem(0);

            //第一次打开初始化数据
            SharedUtil.putInt("cityid", 2);
            SharedUtil.putString("cityname", "北京市");
            SharedUtil.putString("districtname", "全部");
            SharedUtil.putString("latitude", "22.534781");
            SharedUtil.putString("longitude","113.944766");
            SharedUtil.putInt("sort", 1);
            SharedUtil.putInt("job", 0);
        } else {
            radioGroup.setVisibility(View.GONE);
            handler.sendEmptyMessageDelayed(1, 2000);
        }
        for (int i = 0; i < mImageIds.length; i++) {
            RadioButton radioButton = new RadioButton(this);
            // 设置button的id
            radioButton.setId(i);
            // 将button加入到group
            radioGroup.addView(radioButton);
        }
        RadioButton rb = (RadioButton) radioGroup.getChildAt(0);
        rb.setChecked(true);
    }

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            Intent intent = new Intent();
            intent.setClass(WelcomeActivity.this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            getApplicationContext().startActivity(intent);

            WelcomeActivity.this.finish();
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeMessages(1);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        RadioButton rb = (RadioButton) radioGroup.getChildAt(position);
        rb.setChecked(true);
        btn_start.setVisibility(View.GONE);
        if (position == mImageIds.length - 1) {
            btn_start.setVisibility(View.VISIBLE);

        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
